package org.xjtu.weteam.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xjtu.weteam.common.utils.TokenUtils;
import org.xjtu.weteam.mapper.TaskMapper;
import org.xjtu.weteam.model.domain.Task;
import org.xjtu.weteam.model.dto.StatusNumDTO;
import org.xjtu.weteam.model.dto.TaskDTO;
import org.xjtu.weteam.service.DictionaryService;
import org.xjtu.weteam.service.TaskService;
import org.xjtu.weteam.web.controller.DictionaryController;

import java.util.List;
import java.util.Objects;

@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {
    @Autowired
    private TaskMapper taskMapper;

    @Override
    public List<Task> taskSearchAll(Integer projectId) {
        return taskMapper.taskSearchAll(projectId);
    }

    @Override
    public List<TaskDTO> taskSelectAllByUser(String token, String status) {
        Integer userID = TokenUtils.getIdFromToken(token);

        if(Objects.equals(status, "进行中")){
            status = "110";
        } else if(status.equals("已完成")){
            status = "111";
        } else if (status.equals("已过期")) {
            status = "112";
        } else return null;
        return taskMapper.taskSelectAllByUser(userID, status);
    }

    @Override
    public List<StatusNumDTO> selectStatusNum(String token) {
        Integer userID = TokenUtils.getIdFromToken(token);
        return taskMapper.selectStatusNum(userID);
    }

    @Override
    public Integer changeTaskStatus(Integer taskId, String newStatus) {
        return taskMapper.changeTaskStatus(taskId, newStatus);
    }

    @Override
    public Integer createTask(Task task) {
        task.setStatus("110");
        taskMapper.insertTask(task);
        return task.getTaskId();
    }

    @Override
    public Task getByItsId(Integer integer) {
        return taskMapper.selectByItsId(integer);
    }

    @Override
    public void updateParent(Task task) {
        taskMapper.changeTaskParent(task.getTaskId(),task.getPId());

    }

    @Override
    public List<Task> selectSons(Integer taskId) {
        return taskMapper.selectSons(taskId);
    }

    @Override
    public void updateByItsId(Task task) {
        taskMapper.updateByItsId(task);
    }

    @Override
    public List<Task> selectAllParent(Integer projectid, Integer taskId) {
        return taskMapper.selectAllParent(projectid, taskId);
    }
    @Override
    public List<Task> selectAllSon(Integer projectid, Integer taskId) {
        return taskMapper.selectAllSon(projectid, taskId);
    }
}
